import deepCopy from '../../../../hrpub/common/utils/deep-copy';
import {cacheTools} from 'nc-lightapp-front'
import {getQueryCondition} from '../../../../hrpub/common/utils/utils'

export default class PubAction{
    constructor(comp){
        this.comp = comp
    }

    //查询列表数据
    queryTableData = async ({
        conditions, 
        pageInfo, 
        pk_org, 
        billstate, 
        oid,       
    } = {}) => {
        const {props} = this.comp;
        const {search, main, meta, dispatch, editTable} = props;

        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !conditions && (conditions = getQueryCondition(props,'query'));
        !pageInfo && (pageInfo = main.pageInfo);
        !pk_org && (pk_org = main.orgvalue ? main.orgvalue.refpk : '');
        !billstate && (billstate = main.orderStatus);
        !oid && (oid = meta.getMeta()['list'].oid);
        
        let postData = {
            ...conditions, 
            pageInfo: {
                ...pageInfo,
                pageIndex: pageInfo.pageIndex
            }, 
            pk_org, 
            state: billstate
        };
        try {
            let res = await dispatch({
                type: 'main/queryTableData',
                payload: postData   
            });
            if(res.success) {
                if(res.data && res.data.data && res.data.data.list) {
                    let pageInfo = deepCopy(res.data.data.list.pageInfo);
                    await dispatch({
                        type: 'main/update',
                        payload: {
                            pageInfo: {
                                ...pageInfo,
                                pageIndex: parseInt(pageInfo.pageIndex)
                            }
                        }
                    });
                    let d = res.data.data.list 
                    editTable.setTableData('list', d, false);
                    editTable.focusRowByIndex('list',main.focusIndex)
                    if(billstate !== '2'){
                        props.button.setButtonDisabled({
                            confirm: false,
                            print: false,
                            printjjd: false,
                            printzm: false,
                            suspend: false
                        })
                    }else{
                        props.button.setButtonDisabled({
                            print: false,
                            printjjd: false,
                            printzm: false,
                            suspend: false
                        }) 
                    }
                    cacheTools.set('allpks', d.allpks.slice(0, pageInfo.pageSize))
                    console.log(cacheTools.get('allpks'))
                }
                else {
                    editTable.setTableData('list', {rows: []}, false);
                    props.button.setButtonDisabled({
                        confirm: true,
                        print: true,
                        printjjd: true,
                        printzm: true,
                        suspend: true
                    })
                }
            }
        }
        catch(e) {

        }
    }
    getWidgetTableData= async ({
        pageInfo, 
        value       
    } = {}) => {
        const {props} = this.comp;
        const {search, main, meta, dispatch, editTable} = props;
        let todayFlag = null 
        // 保证调用方式有两种，传参，如果没有传惨就从store拿
        !pageInfo && (pageInfo = main.pageInfo);
        !value && (value = main.widgetValue)
        value == 'dismising'?todayFlag = false:todayFlag= true
        let postData = {
            widgetCode:"6008C0C0",
            appCode:"60092080",
            pageCode:"60092080nccloud",
            areaCode:"list",
            todayFlag,
            pageInfo: {
				pageSize: parseInt(pageInfo.pageSize),
				total: parseInt(pageInfo.total),
				totalPage:parseInt(pageInfo.totalPage),
				pageIndex:  parseInt(pageInfo.pageIndex),
			},
        };
        try {
            let res = await dispatch({
                type: 'main/getWidgetTableData',
                payload: postData   
            });
            if(res.success) {
                if(res.data && res.data.list) {
                    let pageInfo = deepCopy(res.data.list.pageInfo);
                    await dispatch({
                        type: 'main/update',
                        payload: {
                            pageInfo: {
                                ...pageInfo,
                                pageIndex: parseInt(pageInfo.pageIndex)
                            }
                        }
                    });
                    let d = res.data.list 
                    editTable.setTableData('list', d, false);
                    editTable.focusRowByIndex('list',main.focusIndex)
                    cacheTools.set('allpks', d.allpks.slice(0, pageInfo.pageSize))
                }
                else {
                    editTable.setTableData('list', {rows: []}, false);
                    props.button.setButtonDisabled({
                        confirm: true,
                        print: true,
                        printjjd: true,
                        printzm: true
                    })
                }
            }
        }
        catch(e) {

        }
    }
}